$(document).ready(function() {
	$("#status").focus(function() {
		$(this).animate({
			height: '40px'
		}, 600);
		if ($(this).hasClass("default-text")) {
			$(this).val("");
			$(this).removeClass("default-text");
		}
	}).blur(function() {
		$(this).animate({
			height: '20px'
		}, 600);
		if ($(this).val() == "") {
			$(this).addClass("default-text");
			$(this).val("Post your status here...");
			$("#statwords").html("0/300");
		}
	}).keyup(function() {
		if($(this).val().length > 300)
			$(this).val($(this).val().substring(0, 300));
		else {
			$("#statwords").html($(this).val().length + "/300");
		}
	});
	
	
	$("#statshare").click(function(evt) {
		evt.preventDefault();
		if ($("#status").val() == "") {
			alert("Post cannot be empty!");
			return;
		}
		
		
		$.ajax({
			url : "postStatus.do?status=" + $("#status").val(),
			dataType : 'json',
			success : function(data) {
				var msg = data['error'];
				if (msg == "") {
					$("#status").val("Status saved...").addClass("default-text");
					return;
				}
				else {
					alert(msg);
				}
			},
			error : function(xhr, testStatus, error) {
				alert("Communication error!");
				return;
			}
		});
	});
	
	loadExpert();
	
	loadConference();
	
	loadPublication();
	
	loadStatus(0);
});

function loadStatus(start) {
	start = parseInt(start);
	var board = $("#updatelist");
	board.append($("<tbody></tbody>").append($("<tr></tr>").append($("<td class='progressbar'></td>").progressbar({
		value : 100
	}))));
	
	$.ajax({
		url : "loadStatus.do?id=" + $("#user").val() + "&start=" + start,
		dataType : 'json',
		success : function(data) {
			board.find("tbody:last").remove();
			if (data == null) {
				board.append($("<tbody><tr><td colspan='3' class='message'>No more status...</td></tr></tbody>"));
				return;
			}
			else {
				var thumb = data['thumb'];
				var user = data['user'];
				var id = data['id'];
				var source = data['data'];
				$.each(source, function(key, status) {
					
					var tbody = $("<tbody></tbody>");
					
					var tr = $("<tr></tr>");
					var td = $("<td></td>");
					var thumbtd = $("<a href='profile.do?id=" + id + "'><img class='smallthumb' src='" + thumb + "' alt='" + user + "'/></a>");
					td.append(thumbtd);
					var eventcon = $("<th colspan='2'><b>" + user + "</b> says:<br/>" + status['status'] + "<br/><font color='#b2b2b2'>" + status['time'] + "</font></th>");
					
					var upmanage = $("<div class='updatemanage'><a href='deleteStatus.do?id=" + status['id'] + "' class='delete-noti'><img src='images/pam/delete.jpg' class='delete'/></a></div>");
					
					$('.delete-noti').live("click", function(evt) {
							evt.preventDefault();
							var block = $(this).parent().parent().parent().parent();
							
							$.ajax({
								url : $(this).attr("href"),
								dataType : 'json',
								success : function(data) {
									if (data == null) {
										alert("Service Error!");
										return;
									}
									else {
										var msg = data['error'];
										if (msg == "") {
											block.fadeOut(800);
											return;
										}
										else {
											alert(msg);
										}
									}
								},
								error : function(xhr, testStatus, error) {
									alert("Communication error!");
									return;
								}
							});
					});
					
					tr.mouseover(function() {
						if (id == $("#account").val())
							upmanage.show();
					}).mouseout(function() {
						if (id == $("#account").val())
							upmanage.hide();
					});
					tr.append(td);
					tr.append(eventcon);
					eventcon.append(upmanage);
					tbody.append(tr);
					
						if (status['comments'] != undefined) {
							var comments = status['comments'];
							$.each(comments, function(index, comment) {
								
								var comtr = $("<tr></tr>");
								var comtd = $("<td></td>");
								var comthumb = $("<a href='profile.do?id=" + comment['uid'] + "'><img class='smallthumb' src='" + comment['thumb'] + "' alt='" + comment['user'] + "'/></a>");
								comtd.append(comthumb);
								var comth = $("<th><b>" + comment['user'] + "</b> : " + comment['comment'] + "<br/><font color='#b2b2b2'>" + comment['time'] + "</font></th>");
								
								var commanage = $("<div class='updatemanage'><a class='deleteComment' href='deleteComment.do?id=" + comment['id'] + "'><img src='images/pam/delete.jpg' class='delete'/></a></div>");
								
								comtr.mouseover(function() {
									if (comment['uid'] == $("#account").val())
										commanage.show();
								}).mouseout(function() {
									if (comment['uid'] == $("#account").val())
										commanage.hide();
								});
					            comtr.append($("<td></td>"));
								comtr.append(comtd);
								comtr.append(comth.append(commanage));
								comtr.insertAfter(tr);
							});
						}
						var upreplytr = $("<tr></tr>");
						var upreplyth = $("<th colspan='2'></th>");
						var upretext = $("<textarea class='default-text reply-box'>Post your reply here...</textarea>");
						upreplyth.append(upretext);
						
						upretext.focus(function() {
							if ($(this).hasClass("default-text")) {
								$(this).val("");
								$(this).removeClass("default-text");
							}
							$(this).animate({
								height: '40px'
							}, 600);
							$(this).parent().find(".reply-btn").show();
						}).blur(function() {
							$(this).animate({
								height: '20px'
							}, 600);
							
							if ($(this).val() == "") {
								$(this).addClass("default-text");
								$(this).val("Post your reply here...");
								$(this).parent().find(".reply-btn").hide();
							}
						});
						
						var uprebutton = $("<a class='btn_blue' href='#'>Reply</a>");
						var uprebutspan = $("<span class='reply-btn'></span>");
						uprebutton.click(function(evt) {
							evt.preventDefault();
							var comment = $(this).parent().parent().find(":first");
							var comboard = $(this).parent().parent().parent();
							if (comment.val() == "") {
								alert("Reply cannot be empty!");
								return;
							}
							
							$.ajax({
								url : "postComment.do?comment=" + comment.val() + "&status=" + status['id'],
								dataType : 'json',
								success : function(data) {
									var msg = data['error'];
									if (msg == "") {
										comment.addClass("default-text");
										comment.val("Post your reply here...");
										addComment(data, comboard);
										uprebutspan.hide();
										return;
									}
									else {
										alert(msg);
									}
								},
								error : function(xhr, testStatus, error) {
									alert("Communication error!");
									return;
								}
							});
						});
						uprebutspan.append(uprebutton);
						upreplyth.append(uprebutspan);
						upreplytr.append($("<td></td>"));
						upreplytr.append(upreplyth);
						tbody.append(upreplytr);
					tbody.find("tr:even > th").addClass("tbl-news-th");
					board.append(tbody);
				});
				start += source.length;
				var loadMore = $("<a class='ls_rslt_more' href='#' start='" + start + "'>More...</a>");
				loadMore.click(function(evt){
					evt.preventDefault();
					loadStatus($(this).attr("start"));
					$(this).parent().parent().parent().hide();
				});
				board.append($("<tbody></tbody>").append($("<tr></tr>").append($("<td colspan='3'></td>").append(loadMore))));
				
			}
		},
		error : function(xhr, testStatus, error) {
			board.append($("<p align='center'>Communication error!</p>"));
			return;
		}
	});
}

function addComment(data, comboard) {
	
	var thumb = data['thumb'];
	var user = data['user'];
	var id = data['id'];
	var source = data['data'];
	
	$.each(source, function(index, comment) {
		
		var comtr = $("<tr></tr>");
		comtr.append($("<td></td>"));
		
		var comtd = $("<td><a href='profile.do?id=" + id +"'><img class='smallthumb' src='" + thumb + "' alt='" + user + "' /></a></td>");
		var comth = $("<th><b>" + user + "</b> : " + comment['comment'] + "<br/><font color='#b2b2b2'>" + comment['time'] + "</font></th>");       
		
		var commanage = $("<div class='updatemanage'><a class='deleteComment' href='deleteComment.do?id=" + comment['id'] + "'><img src='images/pam/delete.jpg' class='delete'/></a></div>");
		
		comtr.mouseover(function() {
			if (id == $("#account").val())
				commanage.show();
		}).mouseout(function() {
			if (id == $("#account").val())
				commanage.hide();
		});
		
		comth.append(commanage);
		comtr.append(comtd);
		comtr.append(comth);
		;
		
		comtr.css("display", "none");
		if (comboard.find("th").hasClass("tbl-news-th")) {
			comth.addClass("tbl-news-th");
			comboard.find("th").removeClass("tbl-news-th");
		}
		else {
			comboard.find("th").addClass("tbl-news-th");
		}
		comtr.insertBefore(comboard);
		comtr.fadeIn(1000);
	});
}

function loadExpert() {
	var board = $("#recexpert");
	board.append("<p align='center'><img scr='images/pam/loading.gif' width='40' height='40'/><br/>Loading recommended experts...</p>");
	$.ajax({
		url : "loadRecommendExpert.do",
		dataType : 'json',
		success : function(data) {
			board.children().remove();
			if (data == null) {
				board.append($("<p align='center'>No recommened expert available.</p>"));
				return;
			}
			else {
				board.append($("<dt>Recommened Experts:</dt>"));
				var source = data['data'];
				$.each(source, function(key, value) {
					var dd = $("<dd></dd>");
					dd.append("<a href='profile.do?id=" + value['id'] + "' target='_blank'>" + value['user'] + "</a>");
					var follow = $("<a class='btn_blue index-follow' href='#'>Follow</a>");
					follow.click(function(evt) {
						evt.preventDefault();
						$.ajax({
							url : "follow.do?type=1&id=" + value['id'],
							dataType : 'json',
							success : function(data) {
								if (data == null) {
									alert("Service Error!");
									return;
								}
								else {
									var msg = data['error'];
									if (msg == "") {
										this.parent().fadeOut(1000, function() {
											if (board.find("dd:visible").length == 0) {
												board.append($("<p align='center'>No recommened expert available.</p>"));
											}
										});
									}
									else {
										alert(msg);
									}
								}
							},
							error : function(xhr, testStatus, error) {
								alert("Communication error!");
								return;
							}
						});
					});
					
					dd.append(follow);
					board.append(dd);
				});
			}
		},
		error : function(xhr, testStatus, error) {
			board.append($("<p align='center'>Communication error!</p>"));
			return;
		}
	});
}
	
function loadConference() {
	var board = $("#recconf");
	board.append("<p align='center'><img scr='images/pam/loading.gif' width='40' height='40'/><br/>Loading recommended conferences...</p>");
	$.ajax({
		url : "loadRecommendConference.do",
		dataType : 'json',
		success : function(data) {
			board.children().remove();
			if (data == null) {
				board.append($("<p align='center'>No recommened conference available.</p>"));
				return;
			}
			else {
				board.append($("<dt>Recommened Conferences:</dt>"));
				var source = data['data'];
				$.each(source, function(key, value) {
					var dd = $("<dd></dd>");
					var name = value['name'];
					
					if (name.length > 45) {
						name = name.substring(0, 42) + "...";
					}
					dd.append("<a href='#'>" + name + "</a>");
					var follow = $("<a class='btn_blue index-follow' href='#'>Follow</a>");
					follow.click(function(evt) {
						$(this).parent().fadeOut(1000, function() {
							if (board.find("dd:visible").length == 0) {
								board.append($("<p align='center'>No recommened publication available.</p>"));
							}
						});
						/*
						evt.preventDefault();
						$.ajax({
							url : "follow.do?type=1&id=" + value['id'],
							dataType : 'json',
							success : function(data) {
								if (data == null) {
									alert("Service Error!");
									return;
								}
								else {
									var msg = data['error'];
									if (msg == "") {
										follow.parent().fadeOut(1000, function() {
											if (board.find("dd:visible").length == 0) {
												board.append($("<p align='center'>No recommened conference available.</p>"));
											}
										});
									}
									else {
										alert(msg);
									}
								}
							},
							error : function(xhr, testStatus, error) {
								alert("Communication error!");
								return;
							}
						});
						*/
					});
					
					dd.append(follow);
					board.append(dd);
				});
			}
		},
		error : function(xhr, testStatus, error) {
			board.append($("<p align='center'>Communication error!</p>"));
			return;
		}
	});
}

function loadPublication() {
	var board = $("#recpub");
	board.append("<p align='center'><img scr='images/pam/loading.gif' width='40' height='40'/><br/>Loading recommended publications...</p>");
	$.ajax({
		url : "loadRecommendPublication.do",
		dataType : 'json',
		success : function(data) {
			board.children().remove();
			if (data == null) {
				board.append($("<p align='center'>No recommened publication available.</p>"));
				return;
			}
			else {
				board.append($("<dt>Recommened Publications:</dt>"));
				var source = data['data'];
				$.each(source, function(key, value) {
					var dd = $("<dd></dd>");
					var name = value['name'];
					
					if (name.length > 45) {
						name = name.substring(0, 42) + "...";
					}
					dd.append("<a href='#'>" + name + "</a>");
					var follow = $("<a class='btn_blue index-follow' href='#'>Follow</a>");
					follow.click(function(evt) {
						$(this).parent().fadeOut(1000, function() {
							if (board.find("dd:visible").length == 0) {
								board.append($("<p align='center'>No recommened publication available.</p>"));
							}
						});
						/*
						evt.preventDefault();
						$.ajax({
							url : "follow.do?type=1&id=" + value['id'],
							dataType : 'json',
							success : function(data) {
								if (data == null) {
									alert("Service Error!");
									return;
								}
								else {
									var msg = data['error'];
									if (msg == "") {
										follow.parent().fadeOut(1000, function() {
											if (board.find("dd:visible").length == 0) {
												board.append($("<p align='center'>No recommened publication available.</p>"));
											}
										});
									}
									else {
										alert(msg);
									}
								}
							},
							error : function(xhr, testStatus, error) {
								alert("Communication error!");
								return;
							}
						});
						*/
					});
					
					dd.append(follow);
					board.append(dd);
				});
			}
		},
		error : function(xhr, testStatus, error) {
			board.append($("<p align='center'>Communication error!</p>"));
			return;
		}
	});
}